using DC2010.Objects;
using DC2010.Objects.Base;
using DC2010.Objects.Data;
using DC2010.Objects.Common;

namespace DC2010.Effects
{
    public class I_Toogle_Effect : EffectLogic
    {
        public I_Toogle_Effect(EffectData ed)
            : base(ed)
        {
            ShortName = "Toggle";
            LongName = "Effect takes all linked objects on target and toogles them. If target can be toggled then it toggles it.";
            EffectType = EffectType.Instant;

            //Info.Params.sParam1_Desc = "Object which executes effect.";
            //Info.Params.sParam2_Desc = "Target object id to toggle.";
        }

        public override void Instant(BaseObject aInvoker, BaseObject aTarget)
        {
            // if i will have this item in hand and i will click on another item those effects are called
            // instant parts, first parameter will be this item, second parameter will be clicked item.
            //
            // so if i take a key into hand and click with this key on keylock effect will be triggered.
            // Instant(this, clicked item)
            //
            // If ill have an effect I_UNLOCK then ill get
            // I_UNLOCK("Emerald key", "Emerald keyhole")
            //
            // "Emerald keyhole" is real object. every object have property "linked objects"
            // that linked object should be implemented IToogle interface.
            // IToogle.Toogle(BaseItem caller, BaseItem toogler);
            // IToogle.Toogle("Emerald keyhole", "Emerald Key");  
            
            //according to analysis both object must be nonempty
            if ((aTarget!=null))
            {
               //flooritem and wallitem are descendants
               if (aTarget is ItemData)
               {
                  ItemData itm = (ItemData)aTarget;
                  
                  //get linked objects (Those objects are already created by InstanceSystem)
                  foreach(ObjectReference s in itm.LinkedObjects)
                  {
                     // get specific linked object which is currently instanciated
                     // ill look in all Dicts, because i do not know type. However
                     // i do not have to know
                     BaseObject bo = Root.I.GameSystem.GetReferencedObject(s);
                                         
                     //does target object implements IToogle?
                     if (bo is IToogle)
                     {
                        //yes?
                        IToogle itg = (IToogle)bo;
                        itg.Toogle(aInvoker, aTarget);
                     }
                  }
               }
             
               //check if target can be toogled
               if (aTarget is IToogle)
               {
                  IToogle itg = (IToogle)aTarget;
                  
                  itg.Toogle(aInvoker, aTarget);
               }
             
            }

            base.Instant(aInvoker, aTarget);
        }

    }
}
